﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Text;

namespace WmsApp.Export
{
    public class  StockTackingImportorFactory
    {
        public static IStockTackingImportor CreateStockTackingImportor()
        {
            return new StockTackingImportor();
        }
    }

    public interface IStockTackingImportor
    {
        List<StockTackingDtlModel> GetImportData(string fileName);
    }

    public class StockTackingImportor : IStockTackingImportor
    {
        public const string FieldNewGoodsCode = "新物料代码";
        public const string FieldTackingQuantity = "盘库数量";
        public const string SheetName = "盘库";

        public List<StockTackingDtlModel> GetImportData(string fileName)
        {
            var list = new List<StockTackingDtlModel>();
            DataTable excelTable = null;
            var ds = new DataSet();

            var conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";" + "Extended Properties=Excel 8.0;");
            conn.Open();
            //var schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            string tableName = @"{0}$";
            tableName = string.Format(tableName, SheetName);
            string strSql = "select * from [" + tableName + "]";
            var myData = new OleDbDataAdapter(strSql, conn);
            myData.Fill(ds, tableName);

            conn.Close();

            excelTable = ds.Tables[tableName];

            foreach (DataRow dataRow in excelTable.Rows)
            {
                if (String.IsNullOrEmpty(dataRow[FieldNewGoodsCode].ToString().Trim()))
                {
                    continue;
                }
                if (String.IsNullOrEmpty(dataRow[FieldTackingQuantity].ToString().Trim()))
                {
                    continue;
                }

                var model = new StockTackingDtlModel
                                {
                                    NewGoodsCode = dataRow[FieldNewGoodsCode].ToString().Trim(),
                                    TackingQuantity = decimal.Parse(dataRow[FieldTackingQuantity].ToString())
                                };
                list.Add(model);
            }
            return list;

        }
    }

    /// <summary>
    /// 盘库导入的至少两个字段
    /// </summary>
    public class  StockTackingDtlModel
    {
        /// <summary>
        /// 新物料代码
        /// </summary>
        public string NewGoodsCode { get; set; }

        /// <summary>
        /// 盘库数量
        /// </summary>
        public decimal TackingQuantity { get; set; }
    }
}
